package com.base.flowable.flowable.api;

import com.base.common.core.domain.entity.SysUser;
import com.base.common.core.domain.model.LoginUser;
import com.base.common.utils.SecurityUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.User;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author : bruce.liu
 * @title: : BaseResource
 * @projectName : flowable
 * @description: TODO
 * @date : 2019/11/2216:18
 */
public class BaseResource {

    @Autowired
    protected RuntimeService runtimeService;

    public User getLoginUser() {
        final SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        User user = null;
        if(sysUser!=null){
            user = new UserEntityImpl();
            user.setId(sysUser.getUserName());
            user.setEmail(sysUser.getEmail());
            user.setFirstName(sysUser.getNickName());
            user.setLastName(sysUser.getUserName());
        }
        return user;
    }

    /**
     * 判断是否挂起状态
     * @param processInstanceId 流程实例id
     * @return
     */
    public boolean isSuspended(String processInstanceId) {
        boolean flag = true;
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        if (processInstance != null){
            flag = !processInstance.isSuspended();
        }
        return flag;
    }
}
